Component({

  properties: {
    show_num: {
      type: Number
    },
    api_host: {
      type: String
    },
    wxapp_img: {
      type: Object
    }
  },

  data: {
    loading: false,
    list: [],
    next: null,
    next_h: '00',
    next_m: '00',
    next_s: '00',
    intervar: null,
    current_h: ''
  },
  methods: {
    onClickUrl: function (e) {
      wx.redirectTo({
        url: e.currentTarget.dataset.url,
      })
    },
    countdown: function (next) {
      var end_time = next
      var curr_time = parseInt(Date.parse(new Date()) / 1000)
      var diff_time = parseInt(end_time - curr_time)
      var h = Math.floor(diff_time / 3600)
      var m = Math.floor((diff_time / 60 % 60))
      var s = Math.floor((diff_time % 60))
      if (h < 10) {
        h = '0' + h
      }
      if (m < 10) {
        m = '0' + m
      }
      if (s < 10) {
        s = '0' + s
      }
      this.setData({
        next_h: h,
        next_m: m,
        next_s: s
      })
      if (diff_time < 1 && this.data.intervar) {
        clearInterval(this.data.intervar)
        this.setData({
          intervar: null
        })
        this.getData()
      }
    },
    getData: function () {
      var self = this
      this.setData({
        loading: true
      })
      var d = new Date()
      this.setData({
        current_h: d.getHours()
      })
      getApp().request({
        url: getApp().api.default.hourly_miaosha,
        method: "GET",
        data: {
          num: this.data.show_num
        },
        success: function (t) {
          if (t.code == 0) {
            self.setData({
              list: t.data.goods_list,
              next: t.data.next
            })
            if (t.data.next) {
              self.countdown(t.data.next)
              var intervar = setInterval(function () {
                self.countdown(t.data.next)
              }, 1000)
              self.setData({
                intervar: intervar
              })
            }
          }
        },
        complete: function () {
          self.setData({
            loading: false
          })
        }
      });
    }
  },
  lifetimes: {
    attached: function () {
      this.getData()
    }
  }
})